package ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

import controller.MobilePlanSellAndBillSystem;
import customexceptions.LineNotExistsException;
import customexceptions.PlanNotExistsException;
import customexceptions.ServiceNotExistsException;
import customexceptions.ServiceNotExistsInPlanException;
import customexceptions.UserNotExistsException;

public class UseLineUI extends javax.swing.JFrame {
	private JTextField jTextFieldServiceName;
	private JLabel jLabelServiceName;
	private JTextField jTextFieldLineNumber;
	private JLabel jLabelLineNumber;
	private JTextField jTextFieldUnits;
	private JLabel jLabelPlanUnits;
	private JButton jButtonAccept;
	private MobilePlanSellAndBillSystem system;

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				CreateTitularLineUI inst = new CreateTitularLineUI();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	public UseLineUI(){
		
	}
	public UseLineUI(MobilePlanSellAndBillSystem system) {
		super();
		initGUI();
		this.system = system;
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);

			{
				jTextFieldLineNumber = new JTextField();
				getContentPane().add(jTextFieldLineNumber);
				jTextFieldLineNumber.setBounds(133, 47, 333, 28);
			}
			{
				jLabelLineNumber = new JLabel();
				getContentPane().add(jLabelLineNumber);
				jLabelLineNumber.setText("Line Number");
				jLabelLineNumber.setBounds(12, 59, 62, 16);
			}	
			{
				jTextFieldServiceName = new JTextField();
				getContentPane().add(jTextFieldServiceName);
				jTextFieldServiceName.setBounds(133, 87, 333, 28);
			}
			{
				jLabelServiceName = new JLabel();
				getContentPane().add(jLabelServiceName);
				jLabelServiceName.setText("Service");
				jLabelServiceName.setBounds(12, 99, 98, 16);
			}	
			
			{
				jTextFieldUnits = new JTextField();
				getContentPane().add(jTextFieldUnits);
				jTextFieldUnits.setBounds(133, 127, 333, 28);
			}
			{
				jLabelPlanUnits = new JLabel();
				getContentPane().add(jLabelPlanUnits);
				jLabelPlanUnits.setText("Units");
				jLabelPlanUnits.setBounds(12, 137, 103, 16);
			}	
			{
				jButtonAccept = new JButton();
				getContentPane().add(jButtonAccept);
				jButtonAccept.setText("Create");
				jButtonAccept.setBounds(203, 283, 48, 23);

				jButtonAccept.addActionListener(new ActionListener()
				{
					public void actionPerformed(ActionEvent evt) 
					{
					
									try {
										system.useLine(jTextFieldLineNumber.getText(),
												jTextFieldServiceName.getText(),
												Integer.parseInt(jTextFieldUnits.getText()));
									} catch (NumberFormatException e) {
										ErrorReportView errorReportView = new ErrorReportView(e.getMessage());
										errorReportView.setVisible(true);
									} catch (LineNotExistsException e) {
										ErrorReportView errorReportView = new ErrorReportView(e.getErrorMessage());
										errorReportView.setVisible(true);
									} catch (ServiceNotExistsException e) {
										ErrorReportView errorReportView = new ErrorReportView(e.getErrorMessage());
										errorReportView.setVisible(true);
									} catch (ServiceNotExistsInPlanException e) {
										ErrorReportView errorReportView = new ErrorReportView(e.getErrorMessage());
										errorReportView.setVisible(true);
									} catch (UserNotExistsException e) {
										ErrorReportView errorReportView = new ErrorReportView(e.getErrorMessage());
										errorReportView.setVisible(true);
									} catch (PlanNotExistsException e) {
										ErrorReportView errorReportView = new ErrorReportView(e.getErrorMessage());
										errorReportView.setVisible(true);
									}
				


							jTextFieldServiceName.setText("");
							jTextFieldLineNumber.setText("");
							jTextFieldUnits.setText("");
					}
				});
				
			}
			pack();
			this.setSize(510, 417);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}